package com.inspinia.base.dao.typehandler;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;


/**
 * 枚举转换类
 * 使用方法：在实体类对象上面添加注解@JsonDeserialize(using = NumberSourceCodeJsonDeserializer.class)
 * Created by zhangyuan on 2017/1/18.
 */
public class NumberSourceCodeJsonDeserializer extends JsonDeserializer{
    @Override
    public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        JsonNode node = jp.getCodec().readTree(jp);
        JsonNode jsonNode = node.get("code");
        if(null != jsonNode){
            int code = jsonNode.intValue();
            return String.valueOf(code);
        }else {
            return  "";
        }
    }

//    @Override
//    public NumberSource deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
//        JsonNode node = jp.getCodec().readTree(jp);
//        JsonNode jsonNode = node.get("code");
//        if(null != jsonNode){
//            int code = jsonNode.intValue();
//            return NumberSource.valueOf(code);
//        }else {
//            String text = node.asText();
//            return NumberSource.valueOf(Integer.valueOf(text));
//        }
//    }
}
